package com.markyao.controller;

import com.markyao.aop.LogAnnotation;
import com.markyao.dao.pojo.User;
import com.markyao.service.ProductService;
import com.markyao.vo.Result;
import com.markyao.vo.parmas.CartParmas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    @LogAnnotation(module = "热门商品",operation = "获得热门商品列表")
    @GetMapping("getHotList")
    public Result getHotList(){

        return productService.findHotList();
    }

    @GetMapping("getNewList")
    public Result getNewList(){

        return productService.findNewList();
    }
    @GetMapping("details/{id}")
    public Result getDetails(@PathVariable("id") Integer id){

        return productService.findDetailsById(id);
    }

    @PostMapping("addCart")
    public Result addCart(@RequestBody CartParmas cartParmas, HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        String username = user.getUsername();
        cartParmas.setCreatedTime(new Date());
        cartParmas.setCreatedUser(username);
        cartParmas.setUid(uid);
        return productService.addCart(cartParmas);
    }
}
